#!/bin/sh
. /etc/profile

workdir=$(dirname $0)
cd $workdir || exit 1

if [[ $# -eq 1 ]]; then
    startday=$(date +%Y%m%d -d "$1")
    endday=$startday
elif [[ $# -eq 2 ]]; then
    startday=$(date +%Y%m%d -d "$1")
    endday=$(date +%Y%m%d -d "$2")
else    
    startday=`date +%Y%m%d --date '-1 day'`
    endday=$startday
fi

while(($startday <= $endday)); do
    echo "==============================${startday}======================"
    filename=/Data/xinran/log/dorafamily/$startday.log
    if [[ ! -e $filename ]]; then 
        echo "ERROR: file $filename not exists."
        exit 1
    fi
    
    hive -e  "load data local inpath '/Data/xinran/log/dorafamily/$startday.log' OVERWRITE into table qyer.xid_doraid partition (stat_date=$startday);"

    if (($? != 0 )); then
    	echo "error occurs when deal with day ${startday}"
    	exit 1
    fi

    echo "==============================finished ${startday}======================"
    startday=$(date +%Y%m%d -d "$startday +1 days" )
done
